//
//  YFGuideFirstController.m
//  SquashSpark
//
//  Created by Coollang on 2017/12/22.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFGuideFirstController.h"
#import "YFLoginViewController.h"
#import "YFGuideViewcontroller.h"

static NSString * const reuseIdentifier = @"GuideFirstCell";

@interface YFGuideFirstController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property (nonatomic, strong) NSArray *pageViewControllers;

@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIButton *signUpButton;
@property (weak, nonatomic) IBOutlet UIButton *signInButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewHeightCon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewBottomCon;
@property (nonatomic, strong)IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
/** 当前页 */
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation YFGuideFirstController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self adaptiveScreen];
//    [UIView animateWithDuration:0.35 animations:^{
//        [self.bottomView layoutIfNeeded];
//        [self.view layoutIfNeeded]
//    }];
    [self creatViewComtrollers];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.bounces = NO;
    self.pageControl.numberOfPages = self.pageViewControllers.count;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.pagingEnabled = YES;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.backgroundColor = kBgThemeColor;
    self.view.backgroundColor = kBgThemeColor;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}
#pragma mark - collection代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.pageViewControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController *vc = [self viewControllerAtIndex:indexPath.row];
    [cell.contentView addSubview:vc.view];
    // 设置frame
    vc.view.frame = cell.bounds;
    if (![self.childViewControllers containsObject:vc]) {
        [self addChildViewController:vc];
    }
    return cell;
}
- (IBAction)pageControlValueChanage:(UIPageControl *)sender {
    if (sender.currentPage > self.currentPage) {
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:sender.currentPage inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }else if (sender.currentPage < self.currentPage){
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:sender.currentPage inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    }
    self.currentPage = sender.currentPage;
}

- (IBAction)signButtonAction:(UIButton *)sender {
    if ([sender isEqual:self.signInButton]) {
        if (self.signButtonClickBlock) {
            self.signButtonClickBlock(kLoginIn);
        }
    }else {
        if (self.signButtonClickBlock) {
            self.signButtonClickBlock(kLoginUp);
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    self.pageControl.currentPage = page;
    self.currentPage = page;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return collectionView.bounds.size;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    cell.alpha = 0.1;
    [UIView animateWithDuration:0.25 animations:^{
        cell.alpha = 1;
    }];
}


// 得到相应的VC对象
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (self.pageViewControllers.count == 0 || index >= self.pageViewControllers.count) {
        return nil;
    }
    return [self.pageViewControllers objectAtIndex:index];
}

// 根据数组元素值，得到下标值
- (NSUInteger)indexOfViewController:(UIViewController *)viewControlller {
    return [self.pageViewControllers indexOfObject:viewControlller];
}
- (void)creatViewComtrollers {
    YFLoginViewController *guideOneVc = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"YFLoginViewController"];
    guideOneVc.isGuideVc = YES;
    [guideOneVc setSignButtonClickBlock:^(kLoginType loginType) {
        
    }];
    NSArray *guideArr  = @[@(kGuideMotionAnalyPage),@(kGuideReal3DPage),@(kGuideVideoPage),@(kGuideCluePage)];
    NSMutableArray *arrM = [NSMutableArray arrayWithObject:guideOneVc];
    for (NSNumber *pageState in guideArr) {
        YFGuideViewcontroller *guideVc = [[YFGuideViewcontroller alloc] initWithNibName:@"YFGuideViewcontroller" bundle:nil];
        guideVc.gudieStatePage = (kGuideStatePage)pageState.integerValue;
//        guideVc.delegate = self;
        [arrM addObject:guideVc];
    }
    self.pageViewControllers = arrM.copy;
}

//屏幕适配
- (void)adaptiveScreen {
    CGFloat screenWidth = MIN(kYFScreenWidth, kYFScreenHeight);
    CGFloat scale = screenWidth/375.0;
    self.bottomViewHeightCon.constant = self.bottomViewHeightCon.constant * scale;
    if (IS_IPAD) {
        self.signInButton.titleLabel.font = kOpenSansFont(OpenSansExtraBold, 24);
        self.signUpButton.titleLabel.font = kOpenSansFont(OpenSansExtraBold, 24);
    }else {
        self.signInButton.titleLabel.font = kOpenSansFont(OpenSansExtraBold, 13);
        self.signUpButton.titleLabel.font = kOpenSansFont(OpenSansExtraBold, 13);
    }
}

@end
